home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5059 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.1 KB  |  89 lines

  1. Path: news1.urz.tu-dresden.de!news
  2. From: Hoang Minh Son <hoang@eatns1.et.tu-dresden.de>
  3. Newsgroups: comp.lang.c++
  4. Subject: Borland C++ 4.5 : delete [] operator - what's going on here?
  5. Date: 2 Feb 1996 10:33:58 GMT
  6. Organization: TU Dresden, IfA
  7. Message-ID: <4espam$ddf@rks1.urz.tu-dresden.de>
  8. NNTP-Posting-Host: 141.30.119.16
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 1.22 (Windows; I; 16bit)
  13.  
  14. HI,
  15. I'm using EasyWin (Borland C++ 4.5) to test my matrix library under Windows 3.1. 
  16. Could anybody tell me what's wrong in the following code?
  17.  
  18. template<class T> class TMatrix
  19. {  
  20.   public:    
  21.         TMatrix(size_t m = 0, size_t n = 0);
  22.     ~TMatrix()    
  23.                 {
  24.         delete[] elem;
  25.         delete[] pcol;
  26.             }
  27.         T& operator()(size_t i, size_t j);
  28.         {
  29.         return pcol[j][i];
  30.         }
  31.  
  32.           //....
  33.   private:
  34.         size_t nrow;
  35.         size_t ncol;
  36.         T**    pcol;           // pointer to columns
  37.         T*     elem;           // element array
  38. };
  39.  
  40. template<class T>
  41. TMatrix<T>::TMatrix(size_e m, size_t n) : nrow(m), ncol(n)
  42. {    
  43.         elem = new T[m*n+1];  //  a dummy space for efficient use 
  44.         pcol = new T*[n+1];   //  of 1-based indexing
  45.             
  46.     if (n > 0)
  47.     {
  48.         pcol[1] = elem;
  49.         for (int i=1; i <= n; i++)
  50.             pcol[i+1] = pcol[i] + m;
  51.     }
  52. }
  53.  
  54. typedef TMatrix<double> Matrix; 
  55.  
  56. void test()
  57. {
  58.     Matrix a(4,4);
  59.     for (int i = 1; i <= 4; i++)
  60.         for (int j = 1; j <= 4; j++)
  61.             a(i,j) = i + j;
  62. }
  63.  
  64. main()
  65. {
  66.     test();
  67.  
  68.     cout << " Oh! What's a trouble ";
  69.     return 0;
  70. }
  71.  
  72.  
  73.  
  74.  
  75. The programm was build without trouble (You can test it by yourself). But as it was 
  76. about to finish running, I got this runtime-error:
  77.                   
  78.                "   Unhandled Exception
  79.                 General Protection Exception
  80.                      0X09FF...
  81.                 ........    Processor Faults   "
  82.  
  83. I debugged and discovered that the 'bug' may be caused on the destruction of the 
  84. matrix 'a', before "Oh! What's a trouble" was displayed. However, if 'a' was 
  85. declared outside 'test()', this "Oh!..." really came before the runtim error. 
  86.  
  87. What am I doing wrong?  Or what's wrong with the delete [] operator?
  88.  
  89.